<?php
class Gd
{
	private $bigWidth;
	private $bigHeight;
	private $isOnlyWidth = false;
	private $bgImageUrl;

	function __construct($isOnlyWidth = false,$width = 80,$height = 80)
	{
		$this->bigWidth = $width;
		$this->bigHeight = $height;
		$this->isOnlyWidth = $isOnlyWidth;
		$this->bgImageUrl = __UPLOAD_DIR."bg.jpg";
	}

	function ResizeImage($file,$smallFile = "")
	{
		$tmpImageInfo = @getimagesize($file);
		$W = $tmpImageInfo[0];
		$H = $tmpImageInfo[1];

		$resizedW = $this->bigWidth;
		$resizedH = $H/$W*$resizedW;
		if ($resizedH > $this->bigHeight && !$this->isOnlyWidth)
		{
			$resizedH = $this->bigHeight;
			$resizedW = $W/$H*$resizedH;
		}
		if (empty($smallFile))
		$this->resize($file, $resizedW, $resizedH,$file);
		else
		$this->resize($file, $resizedW, $resizedH,$smallFile);

		if (!$this->isOnlyWidth)
		{
			if(!@file_exists($this->bgImageUrl))
			{
				$this->createImage($this->bigWidth, $this->bigHeight, $this->bgImageUrl);
			}
			//$this->combineImage($file,$file,$this->bgImageUrl);
			echo $smallFile;
			$this->combineImage($smallFile,$smallFile,$this->bgImageUrl);
		}
	}


	function resize($imagePath, $width, $height, $destPath)
	{
		$tmpImageInfo = @getimagesize($imagePath);
		switch ($tmpImageInfo[2]) {
			case 1:
				//return true;
				$im = @ImageCreateFromGIF($imagePath);
				break;
			case 2:
				$im = @ImageCreateFromJPEG($imagePath);
				break;
			case 3:
				$im = @ImageCreateFromPNG($imagePath);
				break;
		}
		if (@function_exists("ImageCreateTruecolor"))
		{
			$ni =@ImageCreateTruecolor($width,$height);
		}
		else{
			$ni =@ImageCreate($width,$height);
		}
		if (@function_exists("imagecopyresampled"))
		@imagecopyresampled($ni, $im, 0, 0, 0, 0, $width, $height, $tmpImageInfo[0], $tmpImageInfo[1]);
		else
		@ImageCopyResized ($ni, $im, 0, 0, 0, 0, $width, $height, $tmpImageInfo[0], $tmpImageInfo[1]);
		if(!@imagejpeg($ni,$destPath))
		{
			return false;
		}
		@ImageDestroy ($ni);
		@ImageDestroy ($im);
		return true;
	}


	function createImage($w, $h, $dstPath)
	{
		$d_im = imagecreate($w, $h);
		@imagecolorallocate($d_im , 255 , 255 , 255);
		@imagejpeg($d_im, $dstPath);
		@ImageDestroy($d_im);
	}


	function combineImage($imageUrl, $destPath, $bgPicPath)
	{
		$s_imageInfo = @getimagesize($imageUrl);
		switch ($s_imageInfo[2])
		{
			case 1:
				$s_im = @ImageCreateFromGIF($imageUrl);
				break;
			case 2:
				$s_im = @ImageCreateFromJPEG($imageUrl);
				break;
			case 3:
				$s_im = @ImageCreateFromPNG($imageUrl);
				break;
		}
		$s_im_w = $s_imageInfo[0];
		$s_im_h = $s_imageInfo[1];
		$b_imageInfo = @getimagesize($bgPicPath);
		$w = $b_imageInfo[0];
		$h = $b_imageInfo[1];
		$d_im = @ImageCreateFromJPEG($bgPicPath);
		$d_im_x_pos = ($w-$s_im_w)/2;
		$d_im_y_pos	= ($h-$s_im_h)/2;
		$bgcolor = @imagecolorallocate($d_im ,100 , 100 , 100);
		@imagecopy($d_im, $s_im, $d_im_x_pos, $d_im_y_pos, 0, 0, $s_im_w, $s_im_h);
		@ImageDestroy($s_im);
		if (@imagejpeg($d_im, $destPath))
		{
			@ImageDestroy($d_im);
			return true;
		}
		@ImageDestroy($d_im);
		return false;
	}


	function rotatePicture($picturePath,$angle) {
		if (file_exists($picturePath)) {
			$newPicture = imagecreatefromjpeg($picturePath);
			$rpicture = imagerotate($newPicture,$angle,0);
			$fileName = basename($picturePath);
			$seed = substr(md5(uniqid("")), 0, 8);
			$newFileName = $seed.".jpg";
			$newPath = str_replace($fileName,$newFileName,$picturePath);
			@unlink($picturePath);
			imagejpeg($rpicture,$newPath);
			//imagejpeg($rpicture);
			return $newPath;
		} else {
			return "";
		}
	}

}

/********************* Image
include_once("define.php");
include_once(__CLASS_DIR.'/default/image.class.php');

$image = new Image();
$image->ResizeImage("F:/picture/1.jpg");
*/
?>